package com.odobenus.listener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.odobenus.tools.HttpTools;
import com.odobenus.zk.ZooKeeperServiceDiscovery;

public class OdobenusFileListener implements FileAlterationListener {
	
	private String rootDir;
	private ZooKeeperServiceDiscovery zkDiscovery;

	public OdobenusFileListener(String rootDir, ZooKeeperServiceDiscovery zkDiscovery) {
		super();
		this.rootDir = rootDir;
		this.zkDiscovery = zkDiscovery;
	}

	@Override
	public void onStart(FileAlterationObserver fileAlterationObserver) {
		//System.out.println("monitor start scan files..");
	}

	@Override
	public void onDirectoryCreate(File file) {
		System.out.println(file.getName() + " director created.");
	}

	@Override
	public void onDirectoryChange(File file) {
		System.out.println(file.getName() + " director changed.");
	}

	@Override
	public void onDirectoryDelete(File file) {
		System.out.println(file.getName() + " director deleted.");
	}

	@Override
	public void onFileCreate(File file) {
		System.out.println(file.getName() + " created.");
	}

	@Override
	public void onFileChange(File file) {
		List<String> clientAddressList = zkDiscovery.discover();
		for (String clientAddress : clientAddressList) {
			List<NameValuePair> nvps=new ArrayList<NameValuePair>();
			String filePath = file.getPath();
			String simplePath = filePath.substring(this.rootDir.length()+1);
			String app = getName(simplePath,"app","version");
			StringBuilder sb=new StringBuilder();
			sb.append("======================================================================Odobenus START======================================================================");
			sb.append("\napp="+app+",");
			String envName = file.getParentFile().getName();
			String version = getName(simplePath,"version",envName);
			sb.append("version="+version+",");
			sb.append("env="+envName+",");
			sb.append("file="+file.getName());
			sb.append(" changed,start notify");
			System.out.println(sb);
			nvps.add(new BasicNameValuePair("app", app));
			nvps.add(new BasicNameValuePair("version", version));
			nvps.add(new BasicNameValuePair("env", envName));
			nvps.add(new BasicNameValuePair("fileName", file.getName()));
			try {
				String URL=clientAddress+"/fileChange";
				if("apollo.properties".equals(file.getName())) {
					URL=clientAddress+"/fileChangeApollo";
				}
				String res = HttpTools.get(URL, nvps);
				if("SUCCESS".equals(res)) {
					System.out.println("notify success!!");
					System.out.println("======================================================================Odobenus END======================================================================");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	
	}

	private String getName(String simplePath,String firstSymble,String lastSymble) {
		return simplePath.substring(simplePath.indexOf(firstSymble)+firstSymble.length()+1, simplePath.indexOf(lastSymble)-1);
	}

	@Override
	public void onFileDelete(File file) {
		System.out.println(file.getName() + " deleted.");
	}

	@Override
	public void onStop(FileAlterationObserver fileAlterationObserver) {
		//System.out.println("monitor stop scanning..");
	}
}